using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//后台用户登录
namespace ApiProject.Api.Controllers
{

    //管理员登录并获取token
    [ApiController]
    [Route("[controller]")]
    public class AddressController : ControllerBase
    {

        private IRepository<ReceivingIdInfo> _addressRepository;

        private readonly IConfiguration _configuration;

        public AddressController(
            IConfiguration configuration,
            IRepository<ReceivingIdInfo> addressRepository
        )
        {
            _configuration = configuration;
            _addressRepository = addressRepository;
        }

        //查找该用户Id的收获地址
        // Get /address/{id}
        [HttpGet("{id}")]
        public dynamic GetAddress(int id)
        {
            var addressInfo = _addressRepository.Table.Where(x => x.UserId == id && x.IsDelete == false).ToList();

            if (addressInfo == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "暂无数据"
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = addressInfo,
                    Msg = "查找成功"
                });
            }
        }


        //添加该用户id的收货信息
        // Post /address/{id}
        [HttpPost("{id}")]
        public dynamic AddAddress(int id, NewAddress model)
        {

            var recipients = model.Recipients.Trim();
            var telephone = model.Telephone.Trim();
            var address = model.Address.Trim();

            var newAddress = new ReceivingIdInfo
            {
                UserId = id,
                Recipients = recipients,
                Telephone = telephone,
                Address = address
            };

            _addressRepository.Insert(newAddress);

            return JsonHelper.Serialize(new
            {
                Code = 200,
                Data = newAddress,
                Msg = "添加成功"
            });
        }


        // 编辑收货地址
        // Put /address/{id}
        [HttpPut("{id}")]
        public dynamic EditAddress(int id, NewAddress model)
        {
            var isAddress = _addressRepository.GetById(id);

            var recipients = model.Recipients.Trim();
            var telephone = model.Telephone.Trim();
            var address = model.Address.Trim();

            isAddress.Recipients = recipients;
            isAddress.Telephone = telephone;
            isAddress.Address = address;
            isAddress.UpdateTime = DateTime.Now;

            _addressRepository.Update(isAddress);

            return JsonHelper.Serialize(new{
                Code = 200,
                Data = isAddress,
                Msg = "修改成功"
            });
        }


        // 删除收获地址
        // Delete /address/id
        [HttpDelete("{id}")]
        public dynamic DeleteAddress(int id){
            var isAddress = _addressRepository.GetById(id);

            isAddress.IsDelete = true;
            isAddress.UpdateTime = DateTime.Now;

            _addressRepository.Update(isAddress);

            return JsonHelper.Serialize(new{
                Code = 200,
                Data = "",
                Msg = "删除成功"
            });
        }





    }
}
